package com.shop.cereshop.app.dl.logic.controller;

import com.shop.cereshop.app.dl.logic.domian.CareShopNewsViewLog;
import com.shop.cereshop.app.dl.logic.domian.MyAccount;
import com.shop.cereshop.app.dl.logic.service.CareShopNewsViewLogService;
import com.shop.cereshop.app.dl.logic.service.DlCereBuyerUserService;
import com.shop.cereshop.app.dl.logic.service.Impl.SendPostImpl;
import com.shop.cereshop.app.dl.logic.utils.ResultInfo;
import com.shop.cereshop.app.service.buyer.CereBuyerUserService;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.commons.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author: 况鑫威
 * @Date: 2021/6/15
 * @Time: 17:39
 * @ClassName: DlCereBuyerUserController
 * @Description: com.shop.cereshop.app.dl.logic.controller
 */
@RestController
@RequestMapping("/api")
@CrossOrigin
@Slf4j(topic = "DlCereBuyerUserController")
public class DlCereBuyerUserController {
    @Autowired
    private DlCereBuyerUserService dlCereBuyerUserService;
    @Autowired
    private CareShopNewsViewLogService careShopNewsViewLogService;
    @Autowired
    private CereBuyerUserService cereBuyerUserService;
    @Autowired
    private SendPostImpl sendPost;

    /**
     * 功能描述: 绑定手机号
     *
     * @Param: [wechatOpenId, phone, code]
     * @Return: com.shop.cereshop.app.dl.logic.utils.ResultInfo
     * @Author: 况鑫威
     * @Date: 2021/6/15
     * @Time: 18:26
     */
    @PostMapping("/phoneBinding")
    public ResultInfo phoneBinding(String wechatOpenId, String phone, String code) {
        return new ResultInfo(dlCereBuyerUserService.phoneBinding(wechatOpenId, phone, code));
    }


    /**
     * 功能描述: 更新阅读次数
     *
     * @Param: [wechatOpenId, phone, code]
     * @Return: com.shop.cereshop.app.dl.logic.utils.ResultInfo
     * @Author: 况鑫威
     * @Date: 2021/6/15
     * @Time: 18:26
     */
    @PostMapping("/updateRead")
    public ResultInfo updateForward(Long newsId, HttpServletRequest request, Long buyerForwardUserId, String buyerUserName,String time) {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
//        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (newsId == null) {
            return ResultInfo.error("缺少新闻id");
        }
        if (buyerForwardUserId == null) {
            return ResultInfo.error("缺少转发人Id");
        }
        if (buyerUserName == null || "".equals(buyerUserName)) {
            return ResultInfo.error("缺少转发人名称");
        }
        Date date = TimeUtils.yyMMddHHmmss2(time);
        if (user != null) {
            CereBuyerUser user2 =  cereBuyerUserService.selectById(user.getBuyerUserId());
            CareShopNewsViewLog careShopNewsViewLog = new CareShopNewsViewLog();
            careShopNewsViewLog.setViewTime(date);
            careShopNewsViewLog.setNewsId(newsId);
            careShopNewsViewLog.setBuyerViewUserId(user.getBuyerUserId());
            careShopNewsViewLog.setBuyerUserName(user2.getName());
            careShopNewsViewLog.setBuyerForwardUserId(buyerForwardUserId);
            if (careShopNewsViewLogService.insertSelective(careShopNewsViewLog) > 0) {
                return ResultInfo.ok();
            } else {
                return ResultInfo.error("操作失败");
            }
        }else {
            CareShopNewsViewLog careShopNewsViewLog = new CareShopNewsViewLog();
            careShopNewsViewLog.setViewTime(date);
            careShopNewsViewLog.setNewsId(newsId);
            careShopNewsViewLog.setBuyerUserName("游客");
            careShopNewsViewLog.setBuyerViewUserId(0L);
            careShopNewsViewLog.setBuyerForwardUserId(buyerForwardUserId);
            if (careShopNewsViewLogService.insertSelective(careShopNewsViewLog) > 0) {
                return ResultInfo.ok();
            } else {
                return ResultInfo.error("操作失败");
            }
        }
    }


    /**
     * 功能描述: 自动注册
     *
     * @Param: [wechatOpenId, headImage, wechatName, wechatNumber]
     * @Return: com.shop.cereshop.app.dl.logic.utils.ResultInfo
     * @Author: 况鑫威
     * @Date: 2021/6/16
     * @Time: 10:34
     */
//    @PostMapping("/registerVoluntarily")
//    public ResultInfo registerVoluntarily(String wechatOpenId, String headImage, String wechatName, String wechatNumber) {
//        return new ResultInfo(dlCereBuyerUserService.registerVoluntarily(wechatOpenId, headImage, wechatName, wechatNumber));
//    }


    /**
     * 功能描述:用户是否关注公众号
     *
     * @Param: [wechatOpenId]
     * @Return: com.shop.cereshop.app.dl.logic.utils.ResultInfo
     * @Author: 况鑫威
     * @Date: 2021/6/16
     * @Time: 10:49
     */
    @PostMapping("/getUserOpenId")
    public ResultInfo getUserOpenId(String wechatOpenId) {
        return new ResultInfo(dlCereBuyerUserService.getUserOpenId(wechatOpenId));
    }


    /**
     * 功能描述: 我的账户
     *
     * @Param: [buyerUserId]
     * @Return: com.shop.cereshop.app.dl.logic.utils.ResultInfo
     * @Author: 况鑫威
     * @Date: 2021/6/16
     * @Time: 15:19
     */
    @PostMapping("/getMyAccount")
    public ResultInfo getMyAccount(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        MyAccount myAccount = new MyAccount();
        if (user == null) {
            myAccount.setPoints(0);
            myAccount.setCanWithdraw(new BigDecimal(0));
            myAccount.setConditionMoney(new BigDecimal(0));
            myAccount.setCoupon(0);
            return new ResultInfo(myAccount);
        }
        return new ResultInfo(dlCereBuyerUserService.getMyAccount(user.getBuyerUserId()));
    }

    /**
     * 功能描述: 获取卡券列表
     * @Param: [request]
     * @return: com.shop.cereshop.app.dl.logic.utils.ResultInfo
     * @Author: 况鑫威
     * @Date: 2021/7/7
     * @Time: 16:40
     */
    @PostMapping("/getCoupons")
    public ResultInfo<?> getCoupons(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
            return new ResultInfo<>(dlCereBuyerUserService.getCoupons(user.getBuyerUserId()));
        }
        return ResultInfo.error("请先登录");
    }


    /**
     * @param
     * @description:调用产品价格日历
     * @return:
     * @author:作者:YF
     * @time:日期:2021/5/20 时间:15:32
     */
    @PostMapping("/getCalenderPrice")
    @ResponseBody
    public ResultInfo<?> getCalenderPrice(String productNo) {
        String price = sendPost.GetCalenderPrice(Integer.valueOf(productNo));
        return new ResultInfo<>(price);
    }


}
